#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QStringList stringList;
    stringList << "北京" << "江苏" << "安徽" << "浙江" << "上海" << "深圳";
    modelStringList = new QStringListModel(this);
    modelStringList->setStringList(stringList);

    ui->listView->setModel(modelStringList);
    ui->listView->setEditTriggers(QAbstractItemView::SelectedClicked |
                                  QAbstractItemView::DoubleClicked);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_btnAddItem_clicked()
{
    modelStringList->insertRow(modelStringList->rowCount());
    QModelIndex index = modelStringList->index(modelStringList->rowCount() - 1);
    modelStringList->setData(index, "Add new Item");
    ui->listView->setCurrentIndex(index);
}

void Widget::on_btnInsertItem_clicked()
{
    QModelIndex index = ui->listView->currentIndex();
    modelStringList->insertRow(index.row());
    modelStringList->setData(index, "Insert new Item");
    ui->listView->setCurrentIndex(index);
}

void Widget::on_btnRemoveItem_clicked()
{
    QModelIndex index = ui->listView->currentIndex();
    modelStringList->removeRow(index.row());
}

void Widget::on_btnClearList_clicked()
{
    modelStringList->removeRows(0, modelStringList->rowCount());
}

void Widget::on_btnShowList_clicked()
{
    QStringList list = modelStringList->stringList();
    ui->plainTextEdit->clear();
    for (int i = 0; i <list.count(); i++) {
        ui->plainTextEdit->appendPlainText(list.at(i));
    }
}

void Widget::on_btnClearText_clicked()
{
    ui->plainTextEdit->clear();
}

void Widget::on_listView_clicked(const QModelIndex &index)
{
    ui->lCurItmeIndex->setText(QString::asprintf("当前项：row = %d，col = %d", index.row(), index.column()));
}
